package org.apache.http.entity.mime;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.charset.Charset;
import java.util.List;
import org.apache.http.a.c;

abstract class a {
    private static final c b = a(h.a, ": ");
    private static final c c = a(h.a, "\r\n");
    private static final c d = a(h.a, "--");
    protected final Charset a;
    private final String e;
    private final String f;

    public a(String str, Charset charset, String str2) {
        org.apache.http.a.a.a((Object) str, "Multipart subtype");
        org.apache.http.a.a.a((Object) str2, "Multipart boundary");
        this.e = str;
        if (charset == null) {
            charset = h.a;
        }
        this.a = charset;
        this.f = str2;
    }

    private static c a(Charset charset, String str) {
        ByteBuffer encode = charset.encode(CharBuffer.wrap(str));
        c cVar = new c(encode.remaining());
        cVar.a(encode.array(), encode.position(), encode.remaining());
        return cVar;
    }

    private static void a(String str, OutputStream outputStream) throws IOException {
        a(a(h.a, str), outputStream);
    }

    private static void a(String str, Charset charset, OutputStream outputStream) throws IOException {
        a(a(charset, str), outputStream);
    }

    private static void a(c cVar, OutputStream outputStream) throws IOException {
        outputStream.write(cVar.b(), 0, cVar.a());
    }

    protected static void a(i iVar, OutputStream outputStream) throws IOException {
        a(iVar.a(), outputStream);
        a(b, outputStream);
        a(iVar.b(), outputStream);
        a(c, outputStream);
    }

    protected static void a(i iVar, Charset charset, OutputStream outputStream) throws IOException {
        a(iVar.a(), charset, outputStream);
        a(b, outputStream);
        a(iVar.b(), charset, outputStream);
        a(c, outputStream);
    }

    public abstract List a();

    public void a(OutputStream outputStream) throws IOException {
        a(outputStream, true);
    }

    void a(OutputStream outputStream, boolean z) throws IOException {
        c a = a(this.a, b());
        for (b bVar : a()) {
            a(d, outputStream);
            a(a, outputStream);
            a(c, outputStream);
            a(bVar, outputStream);
            a(c, outputStream);
            if (z) {
                bVar.b().writeTo(outputStream);
            }
            a(c, outputStream);
        }
        a(d, outputStream);
        a(a, outputStream);
        a(d, outputStream);
        a(c, outputStream);
    }

    protected abstract void a(b bVar, OutputStream outputStream) throws IOException;

    public String b() {
        return this.f;
    }

    public long c() {
        long j = 0;
        for (b b : a()) {
            long contentLength = b.b().getContentLength();
            if (contentLength < 0) {
                return -1;
            }
            j += contentLength;
        }
        OutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        try {
            a(byteArrayOutputStream, false);
            return j + ((long) byteArrayOutputStream.toByteArray().length);
        } catch (IOException e) {
            return -1;
        }
    }
}
